package com.league.zjxn.main

import com.blankj.utilcode.util.GsonUtils
import com.blankj.utilcode.util.LogUtils
import com.league.zjxn.R
import com.league.zjxn.entity.BannerData
import com.league.zjxn.entity.HomeData
import com.league.zjxn.jetpack.vmodel.HomeViewModel
import com.league.zjxn.main.adapter.HomeTrendDataAdapter
import com.league.zjxn.main.adapter.HomeTrendTabAdapter
import com.league.zjxn.main.home.ContactUsActivity
import com.league.zjxn.main.home.HomeSearchActivity
import com.league.zjxn.main.home.HomeTrendActivity
import com.league.zjxn.main.home.SameDetailsActivity
import com.league.zjxn.main.mine.LeagueNoticeActivity
import com.module.lemlin.http.Status
import com.module.lemlin.http.msg
import com.module.lemlin.image.loadCenterImage
import com.module.lemlin.owner.OwnerViewModeFragment
import com.youth.banner.adapter.BannerImageAdapter
import com.youth.banner.holder.BannerImageHolder
import com.youth.banner.indicator.CircleIndicator
import kotlinx.android.synthetic.main.fragment_main_home.*
import kotlinx.android.synthetic.main.view_home_about.*
import kotlinx.android.synthetic.main.view_home_banner.*
import kotlinx.android.synthetic.main.view_home_intros.*
import kotlinx.android.synthetic.main.view_home_league.*
import kotlinx.android.synthetic.main.view_home_trend_data.*
import kotlinx.android.synthetic.main.view_home_trend_tab.*

class MainHomeFragment : OwnerViewModeFragment<HomeViewModel>() {

    companion object {
        fun newInstance(): MainHomeFragment {
            return MainHomeFragment()
        }
    }

    private val mHomeTrendTabAdapter = HomeTrendTabAdapter()

    private val mHomeTrendDataAdapter = HomeTrendDataAdapter()

    override fun viewModel(): HomeViewModel {
        return createViewModel()
    }

    override val layoutResId: Int
        get() = R.layout.fragment_main_home

    override fun initView() {

        banHomeBannerBody.indicator = CircleIndicator(requireContext())

        rvHomeTrendTabBody.adapter = mHomeTrendTabAdapter
        mHomeTrendTabAdapter.setOnItemClickListener { _, _, position ->
            mHomeTrendTabAdapter.selectPosition = position
            val itemData = mHomeTrendTabAdapter.getItem(position)
            mHomeTrendDataAdapter.setNewInstance(itemData.data)

            LogUtils.e("=====${GsonUtils.toJson(itemData)}")
        }

        rvHomeTrendDataBody.adapter = mHomeTrendDataAdapter

        ivHomeTrendTabMore.setOnClickListener {
            HomeTrendActivity.open()
        }
        ivMainHomeLeagueNotice.setOnClickListener {
            LeagueNoticeActivity.open()
        }

        ivHomeBannerSearch.setOnClickListener {
            HomeSearchActivity.open()
        }
    }

    override fun initData() {
        viewModel.homeIndexApp()
    }

    override fun initViewMode() {
        viewModel.homeDataLiveData.observe(viewLifecycleOwner, { response ->
            if (response.status == Status.FAILURE) {
                toast(response.error?.msg ?: "")
            }
            if (response.status != Status.SUCCESS) {
                return@observe
            }
            val data = response.data?.data
                ?: return@observe
            fillData(data)
        })
    }

    private fun fillData(homeData: HomeData) {
        val bannerData = homeData.banner
        banHomeBannerBody.adapter = object : BannerImageAdapter<BannerData>(bannerData) {
            override fun onBindView(
                holder: BannerImageHolder?, data: BannerData, position: Int, size: Int
            ) {
                holder?.imageView?.loadCenterImage(data.image)
            }
        }

        val trends = homeData.news
        mHomeTrendTabAdapter.setNewInstance(
            trends.take(4).toMutableList()
        )
        mHomeTrendDataAdapter.setNewInstance(
            trends.find { it.id == 0 }?.data ?: mutableListOf()
        )

        val leagueData = homeData.alliance_data
        tvHomeLeagueMemberCount.text = leagueData.alliance_member_count
        tvHomeLeagueProjectCount.text = leagueData.alliance_project_count
        tvHomeLeagueNewsCount.text = leagueData.alliance_news_count
        tvHomeLeagueContent.text = leagueData.alliance_brief_count

        val introsData = homeData.alliance_introduction
        tvHomeIntrosName0.text = if (introsData.size >= 1) introsData[0].name else "联盟介绍"
        tvHomeIntrosName1.text = if (introsData.size >= 2) introsData[1].name else "联盟章程"
        tvHomeIntrosName2.text = if (introsData.size >= 3) introsData[2].name else "联盟公约"
        llHomeIntros0.setOnClickListener {
            if (introsData.size >= 1) {
                SameDetailsActivity.openAlliance(title = "联盟介绍", introsData[0].id)
            }
        }
        llHomeIntros1.setOnClickListener {
            if (introsData.size >= 2) {
                SameDetailsActivity.openAlliance(title = "联盟章程", introsData[1].id)
            }
        }
        llHomeIntros2.setOnClickListener {
            if (introsData.size >= 3) {
                SameDetailsActivity.openAlliance(title = "联盟公约", introsData[2].id)
            }
        }


        val aboutData = homeData.about_us
        tvHomeAboutName0.text = if (aboutData.size >= 1) aboutData[0].name else "关于中交"
        tvHomeAboutName1.text = if (aboutData.size >= 2) aboutData[1].name else "西南区域总部"
        llHomeAbout0.setOnClickListener {
            if (aboutData.size >= 1) {
                SameDetailsActivity.openAbout(title = "中交集团", aboutData[0].id)
            }
        }
        llHomeAbout1.setOnClickListener {
            if (aboutData.size >= 2) {
                SameDetailsActivity.openAbout(title = "西南区域总部", aboutData[1].id)
            }
        }
        llHomeAbout2.setOnClickListener {
            ContactUsActivity.open()
        }

    }
}